package com.tanhua.server.controller;

import com.tanhua.domain.db.PageResult;
import com.tanhua.domain.vo.AcceptMessageVo;
import com.tanhua.server.service.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/messages")
public class MessagesController {

    //注入消息管理业务层
    @Autowired
    private MessagesService messagesService;

    /**
     * 点赞列表
     */
    @GetMapping("/likes")
    public ResponseEntity likeMessage(@RequestParam(defaultValue = "1") Long page,
                                      @RequestParam(defaultValue = "10")Long pagesize)
     {
         PageResult<AcceptMessageVo> voPageResult = getAcceptMessageVoPageResult(page, pagesize,1);

         //b.返回状态码
         return ResponseEntity.ok(voPageResult);
    }

    /**
     * 喜欢列表
     */
    @GetMapping("/loves")
    public ResponseEntity myLikeMessage(@RequestParam(defaultValue = "1") Long page,
                                      @RequestParam(defaultValue = "10")Long pagesize)
    {
        //设置评论的类型
        PageResult<AcceptMessageVo> voPageResult = getAcceptMessageVoPageResult(page, pagesize,3);
        //b.返回状态码
        return ResponseEntity.ok(voPageResult);
    }

    /**
     * 评论列表
     */
    @GetMapping("/comments")
    public ResponseEntity commentsMessage(@RequestParam(defaultValue = "1") Long page,
                                        @RequestParam(defaultValue = "10")Long pagesize)
    {
        //设置评论的类型
        PageResult<AcceptMessageVo> voPageResult = getAcceptMessageVoPageResult(page, pagesize,2);
        //b.返回状态码
        return ResponseEntity.ok(voPageResult);
    }
    /**
     * 抽取公共方法：传入type类型和分页条件查询出分页数据（用户基本信息）
     * @param page
     * @param pagesize
     * @return
     */
    private PageResult<AcceptMessageVo> getAcceptMessageVoPageResult(Long page,Long pagesize,Integer type) {
        //三元运算处理请求参数为null的情况
        page = page > 0 ? page : 1;
        pagesize = pagesize > 0 ? pagesize : 10;
        //a.调用servcie层 传入page,pagesize,type  返回pageReuslt<AcceptMessageVo>
        return messagesService.likeMessage(page, pagesize, type);
    }

}
